package com.ky.manage.activity.indoor;

import android.content.Intent;
import android.util.Log;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ky.manage.R;
import com.ky.manage.adapter.PicShowAdapter;
import com.ky.manage.base.BaseActivity;
import com.ky.manage.model.DocInfo;
import com.ky.manage.model.response.IndoorCheckItemListResp;
import com.ky.manage.model.response.ProjectInfoResp;
import com.ky.manage.utils.http.AsyncHttpUtils;
import com.ky.manage.utils.http.BusinessHttpReqUtils;
import com.loopj.android.http.TextHttpResponseHandler;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cz.msebera.android.httpclient.Header;

/**
 * 检测项展示页
 */
public class IndoorProjectItemInfoActivity extends BaseActivity {
    private IndoorCheckItemListResp.CheckItemInfo checkItemInfo = null;
    private TextView questionStateTv;
    private TextView remarkEt, checkResultEt;
    private RecyclerView picListRecyclerView;
    PicShowAdapter picShowAdapter;

    /**
     *
     * @param baseActivity
     * @param checkItemInfo
     */
    public static void toCheckItemInfoActivity(BaseActivity baseActivity, IndoorCheckItemListResp.CheckItemInfo checkItemInfo) {
        Intent intent = new Intent(baseActivity, IndoorProjectItemInfoActivity.class);
        intent.putExtra("checkItemInfo", checkItemInfo);
        baseActivity.toActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_indoor_project_item_info;
    }

    @Override
    protected void initView() {
        findView(R.id.back_iv, v -> {
            finish();
        });

        questionStateTv = findView(R.id.question_state_tv);
        checkResultEt = findView(R.id.check_result_et);
        remarkEt = findView(R.id.remark_et);
        picListRecyclerView = findView(R.id.pic_list_view);
    }

    @Override
    protected void initData() {
        checkItemInfo = (IndoorCheckItemListResp.CheckItemInfo) getIntent().getSerializableExtra("checkItemInfo");

        if (null == checkItemInfo) {
            return;
        }
        findTextView(R.id.page_title_tv, checkItemInfo.projectName);
        questionStateTv.setText("0".equals(checkItemInfo.isQuestion) ? "是" : "否");
        remarkEt.setText(checkItemInfo.remark);
        checkResultEt.setText(checkItemInfo.checkResult);


        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        picListRecyclerView.setLayoutManager(gridLayoutManager);

        reqGetProjectInfo();
    }


    // 获取检测项目详情
    private void reqGetProjectInfo() {
        if(checkItemInfo == null) {
            return;
        }

        AsyncHttpUtils.getInstance().get(BusinessHttpReqUtils.SERVICE_DOMAIN + "/appApi/getProjectInfo/" + checkItemInfo.reportedId, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Log.d("lhm", "reqGetProjectInfo onFailure responseString=" + responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Log.d("lhm", "reqGetProjectInfo onSuccess responseString=" + responseString);
                ProjectInfoResp resp = new Gson().fromJson(responseString, ProjectInfoResp.class);
                if(resp != null && resp.getCode() == 200) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            questionStateTv.setText(resp.getData().getIsQuestion() == 0 ? "是" : "否");
                            remarkEt.setText(resp.getData().getRemark());
                            checkResultEt.setText(resp.getData().getCheckResult());

                            picShowAdapter = new PicShowAdapter(R.layout.layout_pic_show_item, covert(resp.getData().getDocList()));
                            picListRecyclerView.setAdapter(picShowAdapter);
                            picShowAdapter.notifyDataSetChanged();
                        }
                    });
                } else {
                    Log.d("lhm", "ProjectInfoResp resp is null");
                }
            }
        });
    }

    private List<DocInfo> covert(List<ProjectInfoResp.DataDTO.DocListDTO> list) {
        List<DocInfo> docInfos = new ArrayList<>();

        for(ProjectInfoResp.DataDTO.DocListDTO docListDTO: list) {
            DocInfo info = new DocInfo();
            info.docUrl = docListDTO.getDocUrl();
            Log.d("lhm", "docurl = " + info.docUrl);
            info.docName = docListDTO.getDocName();
            docInfos.add(info);
        }
        return docInfos;

    }
}